﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace Agnes.Core
{
    /// <summary>
    /// 解决方案服务
    /// </summary>
    public class AgnesSlnService
    {
        #region 公开属性

        /// <summary>
        /// 最近访问的解决方案
        /// </summary>
        public List<string> RecentlyUsedSlns { get; set; }
        
        /// <summary>
        /// 当前打开的解决方案,如果没有则为null
        /// </summary>
        public AgnesSln CurrentSln { get; private set; }

        /// <summary>
        /// 解决方案关闭事件
        /// </summary>
        public event EventHandler SlnClosedHandler;

        /// <summary>
        /// 解决方案打开事件
        /// </summary>
        public event EventHandler SlnOpenedHanlder;

        #endregion

        #region 初始化

        internal AgnesSlnService()
        {
            RecentlyUsedSlns = new List<string>();

            //从Data/GobalConfig/RecentlyUsedSln.xml中读取最近使用的方案路径
            XElement el;

            try
            {
                el = XElement.Load(Path.Combine(AgnesServiceManager.DataPath, "GobalConfig\\RecentlyUsedSln.xml"));
                foreach (XElement i in el.Elements("RecentlyUsedSln"))
                {
                    RecentlyUsedSlns.Add(i.Attribute("Path").Value);
                }
            }
            catch
            {
                AgnesServiceManager.LogServ.Warn("Can't Load RecentlyUsedSln!");
                return;
            }
            finally
            {
                AgnesServiceManager.Instance.ShutdownEventHandler += SaveRecentlyUsedSlnList;
            }
        }

        #endregion 

        #region 公开方法

        /// <summary>
        /// 打开某个解决方案
        /// </summary>
        /// <param name="fullPath">方案全路径</param>
        /// <param name="pwd">密码,如果没有则留空</param>
        public void OpenSln(string fullPath, string pwd="")
        {
            if (CurrentSln != null)
            {
                OnSlnClosed(null, null);
            }
            CurrentSln = AgnesSln.UnPackage(fullPath, pwd);
            OnSlnOpened(null, null);

            //保存到最近访问列表中
            if (RecentlyUsedSlns.IndexOf(CurrentSln.FullName) != -1)
            {
                RecentlyUsedSlns.Remove(CurrentSln.FullName);
            }

            RecentlyUsedSlns.Insert(0, CurrentSln.FullName);
            //如果超出10个则把最后一个去掉
            if (RecentlyUsedSlns.Count >= 10)
            {
                RecentlyUsedSlns.RemoveAt(9);
            }
        }

        /// <summary>
        /// 关闭并保存当前文档
        /// </summary>
        public void CloseAndSaveCurrentSln()
        {
            if (CurrentSln != null)
            {
                CurrentSln.Package();
                CurrentSln = null;
            }
        }

        /// <summary>
        /// 保存最近访问列表到文件Data/GobalConfig/RecentlyUsedSln.xml
        /// </summary>
        public void SaveRecentlyUsedSlnList()
        {
            XElement el = new XElement("RecentlyUsedSlns");

            foreach (string i in RecentlyUsedSlns)
            {
                el.Add(new XElement("RecentlyUsedSln", new XAttribute("Path" ,i)));
            }
            
            el.Save(Path.Combine(AgnesServiceManager.DataPath, "GobalConfig\\RecentlyUsedSln.xml"));
        }

        /// <summary>
        /// 方案打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSlnOpened(object sender, EventArgs e)
        {
            if (SlnOpenedHanlder != null)
            {
                SlnOpenedHanlder(sender, e);
            }
        }

        /// <summary>
        /// 方案关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSlnClosed(object sender, EventArgs e)
        {
            if (SlnClosedHandler != null)
            {
                SlnClosedHandler(sender, e);
            }
        }

        #endregion
    }
}
